<?php

namespace App\Service\AppCrash;

use App\Library\FeiShu\SendFs;
use App\Models\AppCrash\AppCrashDetailModel;
use App\Models\AppCrash\AppCrashMainModel;
use App\Models\Common\FsMessageTemplateModel;
use App\Service\BaseService;
use Exception;
use Illuminate\View\Compilers\BladeCompiler;
use Kuafu\GeneralFs\BuildFs;

class AppCrashService extends BaseService
{

    /**
     * @param object $params
     * @throws Exception
     */
    public static function upload(object $params)
    {
        try {
            $nowTime           = time();
            $appCrashMainModel = AppCrashMainModel::query()->create([
                "eventType"  => $params->eventType,
                "timestamp"  => $params->timestamp,
                "isEncrypt"  => $params->isEncrypt,
                "appId"      => attributeGet($params, "eventContent.appId", 0),
                "platformId" => attributeGet($params, "eventContent.platformId", 0),
                "appName"    => attributeGet($params, "eventContent.appName", ""),
                "date"       => attributeGet($params, "eventContent.date", ""),
                "appUrl"     => attributeGet($params, "eventContent.appUrl", ""),
                "signature"  => $params->signature,
                "createdAt"  => $nowTime
            ]);

            AppCrashDetailModel::query()->insert(
                collect(attributeGet($params, "eventContent.datas", []))->map(function ($row) use (
                    $appCrashMainModel,
                    $nowTime
                ) {
                    return [
                        "mainId"     => $appCrashMainModel->id,
                        "tagId"      => attributeGet($row, "tagId", 0),
                        "tagName"    => attributeGet($row, "tagName", ""),
                        "crashCount" => attributeGet($row, "crashCount", 0),
                        "crashUser"  => attributeGet($row, "crashUser", 0),
                        "type"       => attributeGet($row, "type", 0),
                        "url"        => attributeGet($row, "url", ""),
                        "version"    => attributeGet($row, "version", ""),
                        "accessUser" => attributeGet($row, "accessUser", 0),
                        "createdAt"  => $nowTime
                    ];
                })->values()->toArray()
            );

            switch ($appCrashMainModel->eventType) {
                case "bugly_crash_trend":
                    $templateName = "DailyCrashTemplate";
                    break;
                case "bugly_tag":
                    $templateName = "TagCrashTemplate";
                    break;
                default:
                    throw new Exception("事件类型非法");
            }

            $content = view("FsTemplate.Crash.{$templateName}", [
                "main"   => $appCrashMainModel,
                "detail" => $appCrashMainModel->refresh()->appCrashDetailRel,
            ])->render();

            BuildFs::newRobot(SendFs::ROBOT_APP_CRASH, BuildFs::MSG_TYPE_CARD)
                   ->setCardContent(jsonToArr($content, []))
                   ->send();

        } catch (Exception $exception) {
            handleKfException($exception);
        }
    }

}
